/*
 * Copyright (c) 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.ui.model;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import net.sourceforge.perforce.core.PerforceException;
import net.sourceforge.perforce.core.api.IPerforceServer;
import net.sourceforge.perforce.core.resources.RemoteFile;
import net.sourceforge.perforce.core.resources.RemoteFolder;
import net.sourceforge.perforce.core.syncinfo.FolderSyncInfo;
import net.sourceforge.perforce.ui.internal.Policy;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.internal.WorkbenchPlugin;
import org.eclipse.ui.model.IWorkbenchAdapter;

/**
  * @version $Revision: 75 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class ProjectContainerElement 
	extends PerforceModelElement 
	implements IWorkbenchAdapter, IAdaptable
{
	// --------------------------------------------------------------------------- Private Variables
	
	/** Stores the perforce server. */
	private IPerforceServer server;
	
	// ------------------------------------------------------------------------- Public Constructors
	
	/** Constructor for ProjectContainerElement. */
	public ProjectContainerElement(IPerforceServer server)
	{
		this.server = server;
	}
	
	// ---------------------------------------------------------- Public Methods (IWorkbenchAdapter)
	
	/** @see IWorkbenchAdapter#getChildren(java.lang.Object) */
	public Object[] getChildren(Object o) 
	{
		if (!(o instanceof ProjectContainerElement))
		{
			return null;
		}

		final List result = new ArrayList();

		Shell shell = new Shell(Display.getDefault());
		try 
		{
			run(true, false, new IRunnableWithProgress() 
			{
				/** @see IRunnableWithProgress#run(IProgressMonitor) */
				public void run(IProgressMonitor monitor)
					throws InvocationTargetException, InterruptedException 
				{
					try
					{						
						RemoteFile[] files = server.findAllFiles(".project", monitor); //$NON-NLS-1$
						for (int i = 0; i < files.length; i++)
						{
							String folderName =	files[i].getDepotFilename();
							folderName = folderName.substring(0, folderName.lastIndexOf('/'));
							
							String parentName = 
								folderName.substring(0, folderName.lastIndexOf('/'));
							FolderSyncInfo syncInfo = new FolderSyncInfo(parentName);
							RemoteFolder parent =
								new RemoteFolder(server, null, syncInfo, "#head");     //$NON-NLS-1$
								
							syncInfo = new FolderSyncInfo(folderName);
							RemoteFolder folder = 							
								new RemoteFolder(server, parent, syncInfo, "#head");   //$NON-NLS-1$
							result.add(folder);
						}
					}
					catch (PerforceException e)
					{
						throw new InvocationTargetException(e);
					}
				}
			});
		}
		catch (InvocationTargetException e) 
		{
			handle(e);
		} 
		catch (InterruptedException e) 
		{
			handle(e);
		}
		finally
		{
			shell.dispose();
		}

		return result.toArray();
	}
	
	/** @see IWorkbenchAdapter#getImageDescriptor(java.lang.Object) */
	public ImageDescriptor getImageDescriptor(Object object) 
	{
		if (!(object instanceof ProjectContainerElement))
		{
			return null;
		}
		
		return WorkbenchPlugin.getDefault().getSharedImages().getImageDescriptor(
			ISharedImages.IMG_OBJ_PROJECT);
	}
	
	/** @see IWorkbenchAdapter#getLabel(java.lang.Object) */
	public String getLabel(Object o) 
	{
		if (!(o instanceof ProjectContainerElement))
		{
			return null;
		}
		
		return Policy.bind("ProjectContainerElement.getLabel");
	}
	
	/** @see IWorkbenchAdapter#getParent(java.lang.Object) */
	public Object getParent(Object o) 
	{
		return null;
	}
	
	// ----------------------------------------------------------------- Public Methods (IAdaptable)
	
	/** @see org.eclipse.core.runtime.IAdaptable#getAdapter(Class) */
	public Object getAdapter(Class adapter) 
	{
		if (adapter == IWorkbenchAdapter.class)
		{
			return this;
		}
		
		return null;
	}
}
